001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Jan 19, 2003
005     * Time: 7:47:48 PM
006     */
007    
008    package EVolve.util.settings;
009    
010    import EVolve.Scene;
011    import EVolve.data.ElementDefinition;
012    import EVolve.visualization.Visualization;
013    import javax.swing.*;
014    import java.io.*;
015    import java.awt.event.*;
016    import java.awt.*;
017    import java.util.*;
018    
019    public class SceneSetting extends Setting{
020        private static SceneSetting instance = null;
021        private JDialog settingDialog;
022        private JTextField textClassPath, textSourcePath, textVizConfigPath, textVizResultPath, textDefaultDataPath ;
023        private JButton buttonSave, buttonCancel;
024        private ArrayList additionClassPath, sourcePath;
025        private JButton[] buttonDir;
026    
027    
028        private SceneSetting() {
029            /*boolean isRunningUnderWindows = System.getProperty("os.name").startsWith("Windows");
030            String userHome = System.getProperty("user.home");
031            String evolveConfigFile = null;
032            if (userHome == null || userHome.trim().equals("")) {
033                // Fall back to current directory if undefined
034                userHome = ".";
035            }
036    
037            if (!userHome.endsWith(String.valueOf(Character.toString(File.separatorChar)))) {
038                userHome += File.separatorChar;
039            }
040    
041            if (isRunningUnderWindows) {
042                evolveConfigFile = "EVolve.ini";
043            } else {
044                evolveConfigFile = ".EVolve";
045            }
046            iniFilename = userHome + evolveConfigFile;*/
047            iniFilename = getFilenameWithPath("EVolve");
048    
049            contents = new JTextField[5];
050            contents[0] = textDefaultDataPath = new JTextField(12);
051            contents[1] = textVizResultPath = new JTextField(12);
052            contents[2] = textVizConfigPath = new JTextField(12);
053            contents[3] = textClassPath = new JTextField(12);
054            contents[4] = textSourcePath = new JTextField(12);
055    
056            tags = new String[5];
057            tags[0] = "[Default Data Path]";
058            tags[1] = "[Default Viz result path]";
059            tags[2] = "[Default Viz Config path]";
060            tags[3] = "[Additional class path]";
061            tags[4] = "[Source file path]";
062            buttonDir = new JButton[contents.length];
063            for (int i=0; i<buttonDir.length; i++) {
064                buttonDir[i] = new JButton("...");
065                buttonDir[i].setName(String.valueOf(i));
066                buttonDir[i].addActionListener(new ActionListener() {
067                    public void actionPerformed(ActionEvent e) {
068                        addPath();
069                    }
070                });
071            }
072            additionClassPath = new ArrayList();
073            sourcePath = new ArrayList();
074            readDataFromFile();
075        }
076    
077        public static SceneSetting v() {
078            if (instance == null)
079                instance = new SceneSetting();
080            return instance;
081        }
082    
083        public void readDataFromFile() {
084            String line;
085            RandomAccessFile iniFile;
086            File file = new File(iniFilename);
087            if (!file.exists()) return;
088    
089            try {
090                iniFile = new RandomAccessFile(file,"r");
091                line = iniFile.readLine();
092                /** ini file is organized as:
093                 *  [Default Data Path]
094                 *  [Viz result path]
095                 *  [Viz Config path]
096                 *  [Additional class path]
097                 *  [Source file path]
098                 */
099                while (line!=null) {
100                    line = iniFile.readLine();
101                    if (line.length() != 0) {
102                        textDefaultDataPath.setText(line);
103                    }
104    
105                    line = iniFile.readLine(); //skip one tag
106                    line = iniFile.readLine();
107                    if (line.length() != 0) {
108                        textVizResultPath.setText(line);
109                    }
110    
111                    line = iniFile.readLine(); //skip one tag
112                    line = iniFile.readLine();
113                    if (line.length() != 0) {
114                        textVizConfigPath.setText(line);
115                    }
116    
117                    line = iniFile.readLine(); //skip one tag
118                    line = iniFile.readLine();
119                    if (line.length() != 0) {
120                        additionClassPath.clear();
121                        textClassPath.setText(line);
122                        StringTokenizer token = new StringTokenizer(line+File.pathSeparator,File.pathSeparator);
123                        while (token.hasMoreTokens()) {
124                            additionClassPath.add(token.nextToken());
125                        }
126                    }
127    
128                    line = iniFile.readLine(); //skip one tag
129                    line = iniFile.readLine();
130                    if (line.length() != 0) {
131                        sourcePath.clear();
132                        textSourcePath.setText(line);
133                        StringTokenizer token = new StringTokenizer(line+File.pathSeparator,File.pathSeparator);
134                        while (token.hasMoreTokens()) {
135                            sourcePath.add(token.nextToken());
136                        }
137                    }
138                    line = iniFile.readLine();
139                }
140            } catch (IOException e) {
141                Scene.showErrorMessage("Unable to read EVolve setting file \""+iniFilename+"\"");
142                return;
143            }
144            Scene.getUIManager().setLastConfigDir(textVizConfigPath.getText());
145            Scene.getUIManager().setLastDataDir(textDefaultDataPath.getText());
146            Scene.getUIManager().setLastResultDir(textVizResultPath.getText());
147        }
148    
149        public void showSettingDialog() {
150            Box boxMain = new Box(BoxLayout.Y_AXIS);
151            dialog = new JDialog(Scene.getFrame(), "Set path...", true);
152            dialog.setSize(600,300);
153    
154            for (int i=0; i<tags.length; i++) {
155                String prompt = tags[i].substring(1,tags[i].length()-1) + ":";
156                boxMain.add(createPathItem(prompt, contents[i],buttonDir[i]));
157                boxMain.add(Box.createVerticalStrut(20));
158            }
159    
160            Box boxSaveCancel = new Box(BoxLayout.X_AXIS);
161            buttonSave = new JButton("Save");
162            buttonSave.addActionListener(new ActionListener() {
163                public void actionPerformed(ActionEvent e) {
164                    save();
165                    dialog.setVisible(false);
166                }
167            });
168    
169            buttonCancel = new JButton("Cancel");
170            buttonCancel.addActionListener(new ActionListener() {
171                public void actionPerformed(ActionEvent e) {
172                    dialog.setVisible(false);
173                }
174            });
175            boxSaveCancel.add(Box.createHorizontalStrut(40));
176            boxSaveCancel.add(buttonSave);
177            boxSaveCancel.add(Box.createHorizontalStrut(25));
178            boxSaveCancel.add(buttonCancel);
179    
180            boxMain.add(boxSaveCancel);
181            dialog.getContentPane().add(boxMain);
182            Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight());
183        }
184    
185        private Box createPathItem(String prompt, JTextField item, JButton button) {
186            Box itemBox = new Box(BoxLayout.X_AXIS);
187    
188            itemBox.add(new JLabel(prompt));
189            itemBox.add(Box.createHorizontalStrut(15));
190            itemBox.add(item);
191            itemBox.add(Box.createHorizontalStrut(10));
192            itemBox.add(button);
193            return itemBox;
194        }
195    
196        public void showCurrentSetting() {
197            createSettingDialog();
198        }
199    
200        private void createSettingDialog() {
201            Box boxMain = new Box(BoxLayout.Y_AXIS);
202            String setting;
203            int index;
204    
205            settingDialog = new JDialog(Scene.getFrame(), "Current Setting", true);
206            settingDialog.setSize(500,500);
207    
208            // show data source and its path
209            setting = Scene.getCurrentDataFilename();
210            index = setting.lastIndexOf(File.separatorChar);
211            if (index == -1) { // have not loaded date yet
212                boxMain.add(createInfoItem("Data source name:", "N/A"));
213                boxMain.add(Box.createVerticalStrut(20));
214                boxMain.add(createInfoItem("Data source path:", "N/A"));
215            } else {
216                boxMain.add(createInfoItem("Data source name:", setting.substring(index+1)));
217                boxMain.add(Box.createVerticalStrut(20));
218                boxMain.add(createInfoItem("Data source path:", setting.substring(0,index)));
219            }
220            boxMain.add(Box.createVerticalStrut(20));
221    
222            // show path info
223            for (int i=0; i<tags.length; i++) {
224                String prompt = tags[i].substring(1,tags[i].length()-1) + ":";
225                boxMain.add(createInfoItem(prompt, contents[i].getText()));
226                boxMain.add(Box.createVerticalStrut(20));
227            }
228    
229            // show current visualization info
230            HashMap config = readVizSetting();
231            if (config != null) {
232                boxMain.add(createInfoItem("Visualization: ", (String)config.get("Name")));
233                boxMain.add(createInfoItem("Subject: ",((ElementDefinition)config.get("Subject")).getName()));
234                EVolve.visualization.Dimension[] dims = (EVolve.visualization.Dimension[])config.get("Dimension");
235                for (int i=0; i<dims.length; i++)
236                    boxMain.add(createInfoItem("Dimension "+(char)(88+i)+" ",dims[i].getName()));
237                boxMain.add(createInfoItem("Interval",config.get("Interval").toString()));
238            } else {
239                boxMain.add(createInfoItem("Visualization: ", "N/A"));
240                boxMain.add(createInfoItem("Subject: ","N/A"));
241                for (int i=0; i<3; i++)
242                    boxMain.add(createInfoItem("Dimension "+(char)(88+i)+" ","N/A"));
243                boxMain.add(createInfoItem("Interval","N/A"));
244            }
245    
246    
247            // create button box
248            JButton buttonOK = new JButton("OK");
249            buttonOK.addActionListener(new ActionListener() {
250                public void actionPerformed(ActionEvent e) {
251                    settingDialog.setVisible(false);
252                }
253            });
254            JPanel panelButton = new JPanel(new BorderLayout());
255            panelButton.add(buttonOK,BorderLayout.CENTER);
256            boxMain.add(panelButton);
257    
258            settingDialog.getContentPane().add(boxMain);
259            Scene.getUIManager().showDialog(settingDialog, settingDialog.getWidth(), settingDialog.getHeight());
260        }
261    
262        private Box createInfoItem(String prompt, String info) {
263            Box item = new Box(BoxLayout.X_AXIS);
264            item.add(new JLabel(prompt));
265            item.add(Box.createHorizontalStrut(15));
266            item.add(new JTextField(info));
267            return item;
268        }
269    
270        private void addPath() {
271            int activeButtonId = 0;
272            for (int i=0; i<buttonDir.length; i++) {
273                if (buttonDir[i].isFocusOwner()) {
274                    activeButtonId = i;
275                    break;
276                }
277            }
278            JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastDataDir());
279            String newPath;
280    
281            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
282    
283            if(fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
284                newPath = fc.getSelectedFile().getAbsolutePath();
285                switch (activeButtonId) {
286                    case 3:
287                    case 4:
288                        String oldPath = contents[activeButtonId].getText();
289                        if (oldPath.length() != 0)
290                            newPath = oldPath + File.pathSeparator + newPath;
291                    case 0:
292                    case 1:
293                    case 2:
294                        contents[activeButtonId].setText(newPath);
295                        break;
296                }
297            }
298    
299        }
300    
301        public ArrayList getAdditionalClassPath() {
302            return additionClassPath;
303        }
304    
305        public ArrayList getSourcePath() {
306            return sourcePath;
307        }
308    
309        private HashMap readVizSetting() {
310            ArrayList list = Scene.getVisualizationManager().getVisualizationList();
311    
312            Visualization visual = null;
313            for (int i=0; i<list.size();i++) {
314                visual = (Visualization)list.get(i);
315                if (visual.getWindow().isSelected()) break;
316            }
317    
318            if (visual != null)
319                return visual.getCurrentConfigure();
320            else
321                return null;
322       }
323    
324    }
325